<?php

namespace app\admin\model;

use think\Model;

class ConfigValue extends Model
{

    public static $config_table = 'config';
    public static $config_value_table = 'config_value';

    public static function setValues($params)
    {
        foreach ($params as $label => $param) {
            self::where(['label'=>$label])->save(['value'=>$param]);
        }
    }

    public static function getBrandConfig($config)
    {
        if (!$config) return "";
        $arr = array_filter(explode('.',$config));
        $label = "";
        if (count($arr) >= 2){
            $config_code = $arr[0];
            $label = $arr[1];
        }else{
            $config_code = $config;
        }
        $config_id = self::name(self::$config_table)->where('config_code',$config_code)->value('config_id');
        if ($label){
            return self::name(self::$config_value_table)->where(['config_id'=>$config_id,'label'=>$label])->value('value');
        }else{
            return self::name(self::$config_value_table)->where(['config_id'=>$config_id])->column('value','label');
        }

    }

    public static function addonConfig($addon, $config)
    {

        $addon_config = get_addon_info($addon);
        $table_prefix = $addon_config['table_prefix'] ?? '';
        if ($table_prefix){
            if(strpos(self::$config_table,$table_prefix) !== 0){
                self::$config_table = $table_prefix . self::$config_table;
            }
            if(strpos(self::$config_value_table,$table_prefix) !== 0){
                self::$config_value_table = $table_prefix . self::$config_value_table;
            }

        }

       return self::getBrandConfig($config);
    }







}